//三数之和 力扣15
//for循环加双指针 去重  去重顺序 边界判断 
class Solution {
public:
	vector<vector<int>> threeSum(vector<int>& a) {
		vector<vector<int>> ans;
		sort(a.begin(),a.end());
		for(int i = 0; i < a.size()-2; i++)
		{
			if(a[i] > 0) return ans;;
			if(i > 0 && a[i-1] == a[i]) continue;
			int l = i + 1 ;
			int r = a.size() - 1;
			while(l<r)
			{
				if(a[i]+a[l]+a[r]>0) r--;
				else if(a[i]+a[l]+a[r]<0) l++;
				else 
				{
					ans.push_back(vector<int>{a[i],a[l],a[r]});
					
					while(l<r && a[l] == a[l+1]) l++;
					while(l<r && a[r] == a[r-1]) r--;
					
					l++;
					r--;
				}
			}
			
		}
		return ans;
	}
};
